package io.github.wanggit.antcms.server.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wanggit.antcms.server.Constants;
import io.github.wanggit.antcms.server.domain.AntTag;
import io.github.wanggit.antcms.server.service.TagService;
import io.github.wanggit.antcms.server.web.utils.Util;
import io.github.wanggit.antcms.server.web.vm.ListTagsVM;
import io.github.wanggit.antcms.server.web.vm.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("tag")
public class TagController {

    @Autowired private TagService tagService;

    @PostMapping("list")
    public Result listTags(@RequestBody ListTagsVM listTagsVM) {
        Page<AntTag> tags =
                tagService.listTags(
                        null == listTagsVM.getPage() || listTagsVM.getPage() <= 0
                                ? 1
                                : listTagsVM.getPage(),
                        null == listTagsVM.getSize() || listTagsVM.getSize() <= 0
                                ? Constants.DEFAULT_PAGE_SIZE
                                : listTagsVM.getSize(),
                        Util.valid(listTagsVM.getName()) ? listTagsVM.getName().trim() : null,
                        Util.valid(listTagsVM.getAliasName()) ? listTagsVM.getAliasName() : null);
        return Result.ok(tags);
    }

    @PostMapping("sou")
    public Result saveOrUpdateTag(@RequestBody AntTag tag) {
        tagService.saveOrUpdateTag(tag);
        return Result.ok(tag);
    }

    @GetMapping("detail")
    public Result detailTag(@RequestParam("id") Long id) {
        return Result.ok(tagService.getById(id));
    }

    @GetMapping("del")
    public Result deleteTag(@RequestParam("id") Long id) {
        tagService.removeById(id);
        return Result.ok();
    }

    @PostMapping("mdel")
    public Result deleteTags(@RequestBody List<Long> ids) {
        tagService.removeByIds(ids);
        return Result.ok();
    }
}
